package com.lingyi.web.controller.wechat;

import com.lingyi.common.core.domain.R;
import com.lingyi.framework.web.service.WechatService;
import com.lingyi.system.domain.Message;
import com.lingyi.system.domain.MessageRecord;
import com.lingyi.system.domain.PublicUser;
import com.lingyi.system.domain.vo.MaterialLibVo;
import com.lingyi.system.domain.vo.MessageVo;
import com.lingyi.system.service.IMessageRecordService;
import com.lingyi.system.service.IMessageService;
import com.lingyi.system.service.IPublicUserService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @PackageName: com.lingyi.web.controller.wechat
 * @ClassName: MessageController
 * @Description:
 * @Author: czl
 * @Date: 2025/4/19 23:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wechat/message")
public class MessageController {

    private static Logger logger = LoggerFactory.getLogger(MessageController.class);

    private final IMessageService messageService;
    private final WechatService wechatService;
    private final IPublicUserService publicUserService;
    private final IMessageRecordService messageRecordService;

    @PostMapping("/release")
    public R<?> release(@RequestBody Message message) {
        boolean saveSuccess = messageService.save(message);
        if (!saveSuccess) {
            return R.fail("消息保存失败");
        }

//        List<PublicUser> publicUsers = publicUserService.list();
//        if (CollectionUtils.isEmpty(publicUsers)) {
//            return R.ok("没有用户需要发送消息");
//        }
//
//        List<MessageRecord> messageRecords = new ArrayList<>();
//
//        for (PublicUser publicUser : publicUsers) {
//            String openId = publicUser.getOpenId();
//            try {
//                String response = wechatService.sending(openId, message);
//                if (response != null && !response.isEmpty()) {
//                    MessageRecord messageRecord = new MessageRecord();
//                    messageRecord.setMessageId(message.getId());
//                    messageRecord.setOpenId(openId);
//                    messageRecords.add(messageRecord);
//                }
//            } catch (Exception e) {
//                logger.error("发送消息给 {} 失败: {}", openId, e.getMessage(), e);
//            }
//        }
//
//        boolean recordsSaved = messageRecordService.saveBatch(messageRecords, 1000);
        return R.ok("消息记录保存成功");
//        if (saveSuccess) {
//            return R.ok("消息记录保存成功");
//        } else {
//            return R.fail("消息记录保存失败");
//        }
    }

    @PostMapping("/selectPage")
    public R<?> selectPage(@RequestBody MessageVo search){
        return R.ok(messageService.selectPage(search));
    }

    @PostMapping("/removeBatch/{messageIds}")
    public R<?> removeBatch(@PathVariable Long[] messageIds){
        boolean removedFlag = messageService.removeBatchByIds(Arrays.asList(messageIds));
        if (removedFlag) messageRecordService.removeByMessageIds(messageIds);
        return R.ok(removedFlag);
    }
}
